<?php
include_once('./simple_html_dom.php');

// Make a MySQL Connection
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("otafood") or die(mysql_error());

// Get the http GET parameters
$lat = $_GET["lat"];
$long = $_GET["long"];

// Making the xml object
$xml = new  SimpleXMLElement('<foodlist></foodlist>');

// Getting all the food item from the database that are available today 
$result = mysql_query("SELECT food, restaurant, rating FROM foodlist WHERE available_today=1");
if(!$result) {
	$xml->addChild('success', '-1');
}
else {
	$row = mysql_fetch_row($result);
	if($row[0] === null) {
		// No existing records for today's items
		$xml->addChild('success', '0');
	}
	else {
		if($lat === null && $long === null){
			// Output the records in the form of the xml
			// For now tomorrow's menu is same as that of today's
			$xml->addChild('success', '1');
			$today_menu = $xml-> addChild('today');
			$tomorrow_menu = $xml-> addChild('tomorrow');
			while ($row = mysql_fetch_assoc($result)) {
				$foodele = $today_menu->addChild('food');
				$foodele->addChild('name', $row["food"]);
				$foodele->addChild('restaurant', $row["restaurant"]);
				$foodele->addChild('rating', $row["rating"]);
				
				$foodele_tomorrow = $tomorrow_menu->addChild('food');
				$foodele_tomorrow->addChild('name', $row["food"]);
				$foodele_tomorrow->addChild('restaurant', $row["restaurant"]);
				$foodele_tomorrow->addChild('rating', $row["rating"]);
			}
		}
		else{
			// Following are the known locations
			// 0: Dipoli: 60.18542, 24.831178
			// 1: Electrical Engineering: 60.188951, 24.830132
			// 2: CSE Building: 60.187372, 24.822353
			// 3: Restaurant Kasper: 60.183035, 24.826355
			// 4: Mechanical Engineering: 60.186801, 24.826956
			// 5: Restaurant Kvarkki: 60.188481, 24.82922
			// 6: Restaurant Alvari: 60.186833, 24.827031
			// 7: TUAS Building: 60.186668, 24.821033
			// 8: TKK Puu2: 60.180282, 24.824982
			// 9: Taffa: 60.186321, 24.832406 
			
			
			// Output the records in the form of the xml
			$xml->addChild('success', '1');
			// Distances from our current location
			$distance = array(
							"Kvarkki" => (60.188481 - $lat)*(60.188481 - $lat) + (24.82922 - $long)*(24.82922 - $long),
							"TUAS-talo" => (60.186668 - $lat)*(60.186668 - $lat) + (24.821033 - $long)*(24.821033 - $long),
							"Alvari" => (60.186833 - $lat)*(60.186833 - $lat) + (24.827031 - $long)*(24.827031 - $long),
							"Puu" => (60.180282 - $lat)*(60.180282 - $lat) + (24.824982 - $long)*(24.824982 - $long),
							"Täffä" => (60.186321 - $lat)*(60.186321 - $lat) + (24.832406 - $long)*(24.832406 - $long),
							"Teekkariravintolat (Dipoli, Sähkö, Kone, Tieto, Kasper)" => (60.18542 - $lat)*(60.18542 - $lat) + (24.831178 - $long)*(24.831178 - $long)
							);
			// Sorting the distances from our current location to the know restaurant locations
			asort($distance);
			
			// Storing the result from the mysql in a temporary array
			$food_items = array();
			while($row = mysql_fetch_assoc($result)) {
				$food_items[] = $row;
			}
			
			// Adding the result to the xml in sorted manner
			foreach ($distance as $key => $val) {
				$today_menu = $xml-> addChild('today');
				$tomorrow_menu = $xml-> addChild('tomorrow');
				foreach($food_items as $key_ => $val_) {
					if ($key == trim($val_["restaurant"])){
						$foodele = $today_menu->addChild('food');
						$foodele->addChild('name', trim($val_["food"]));
						$foodele->addChild('restaurant', trim($val_["restaurant"]));
						$foodele->addChild('rating', $val_["rating"]);
						
						$foodele_tomorrow = $today_menu->addChild('food');
						$foodele_tomorrow->addChild('name', trim($val_["food"]));
						$foodele_tomorrow->addChild('restaurant', trim($val_["restaurant"]));
						$foodele_tomorrow->addChild('rating', $val_["rating"]);
						
						unset($food_items[$key_]);
					}
				}
			}
			
			// Adding the restaurant with unknown locations to the xml
			foreach($food_items as $key_ => $val_) {
				$foodele = $today_menu->addChild('food');
				$foodele->addChild('name', trim($val_["food"]));
				$foodele->addChild('restaurant', trim($val_["restaurant"]));
				$foodele->addChild('rating', $val_["rating"]);
				
				$foodele_tomorrow = $today_menu->addChild('food');
				$foodele_tomorrow->addChild('name', trim($val_["food"]));
				$foodele_tomorrow->addChild('restaurant', trim($val_["restaurant"]));
				$foodele_tomorrow->addChild('rating', $val_["rating"]);
				
				unset($food_items[$key_]);
			}
		}
	}
}

header ("Content-Type:text/xml"); 
echo $xml->asXML();

?>
